<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Cat;
use App\Good;
use DB;
use Route;
use App\Order;
use App\Customers;
/**
 * 商品控制器
 */

class GoodsController extends Controller{
    /**
     * 展示添加商品模板
     */
    public function gadd(){
        $catlist = $this->getTree();
        $tishiinfo = '<div class="alert alert-info">
				<a class="close" data-dismiss="alert" href="#">x</a>
				<h4 class="alert-heading">添加商品</h4>
				这个页面用来添加商品
		</div>';
        return view('admin/goodsadd',['catlist'=>$catlist,'tishiinfo'=>$tishiinfo]);
    }

    /**
     * 接受添加商品传过来的参数
     */
    public function gaddpost(Request $req){
        $catlist = $this->getTree();
        $goods = new Good();
        $goods->goods_name = $req->goods_name;
        $goods->goods_sn = $req->goods_sn;
        //加入判断货号是否存在
        $goods->cat_id = $req->cat_id;
        $goods->goods_img = $req->goodspic;
        $goods->shop_price = $req->shop_price*100;
        $goods->market_price = $req->market_price*100;
        $goods->goods_desc = $req->content;
        $goods->add_time = time();
        if($goods->save()){
            $tishiinfo = '<div class="alert alert-success">
				<a class="close" data-dismiss="alert" href="#">x</a>
				<h4 class="alert-heading">添加成功!</h4>
				名为:'.$req->goods_name.' 的商品已添加成功!
		</div>';
            return view('admin/goodsadd',['tishiinfo'=>$tishiinfo,'catlist'=>$catlist,'tishiinfo'=>$tishiinfo]);
        }
    }

    public function glist($cat_id = 0){
        $goods = new Good();
        $cats = new Cat();
        $goodslist = [];
        $catlist = $this->getTree();
        //cat_id为0 则查出所有商品   不为0则查出相应栏目下商品
        if($cat_id != 0){
            $goodslist = $goods->join('cats','cats.cat_id','=','goods.cat_id')->where('goods.cat_id','=',$cat_id)->where('is_delete','=',0)->select(['goods.*','cats.cat_name'])->paginate(10);
        }else{
            $goodslist = $goods->join('cats','cats.cat_id','=','goods.cat_id')->where('is_delete','=',0)->select(['goods.*','cats.cat_name'])->paginate(10);
        }
        return view('admin/goodslist',['goods'=>$goodslist,'catlist'=>$catlist]);
    }

    public function gdel($gid){
        $goods = Good::find($gid);
        $goods->is_delete = 1;
        $rs = $goods->save();
        return $rs?redirect('admins/glist'):'删除失败';
    }

    public function gedit($gid){
        //查出分类列表
        $catlist = $this->getTree();
        //查出对应商品
        $goods = Good::where('gid','=',$gid)->first();
        return view('admin/goodsedit',['catlist'=>$catlist,'good'=>$goods]);
    }

    public function geditpost(Request $req){
        $goods = Good::find($req->gid);
        $goods->goods_name = $req->goods_name;
        $goods->goods_sn = $req->goods_sn;
        $goods->cat_id = $req->cat_id;
        $goods->goods_number = $req->goods_number;
        $goods->shop_price = $req->shop_price*100;
        $goods->market_price = $req->market_price*100;
        $goods->goods_desc = $req->content;
        $goods->last_update = time();
        $rs = $goods->save();
        if($rs){
            $tishiinfo = '<div class="alert alert-success">
				<a class="close" data-dismiss="alert" href="#">x</a>
				<h4 class="alert-heading">修改成功!</h4>
				'.$req->goods_name.' 已修改成功!
		</div>';
            return view('admin/goodslist',['tishiinfo'=>$tishiinfo]);
        }
    }

    //无限极分类
    private function getTree($parent_id=0){
        $cat=DB::table('cats')->where('parent_id',$parent_id)->get();
        //遍历分类
        $arr=array();
        if($cat){
            foreach($cat as $key=>$value){
                $value=(array)$value;
                $tmp['cat_id']=$value['cat_id'];
                $tmp['cat_name']=$value['cat_name'];
                $tmp['parent_id']=$value['parent_id'];
                $tmp['sbu']=empty($this->getTree($tmp['cat_id']))?[]:$this->getTree($tmp['cat_id']);
                $arr[]=$tmp;
            }
            return $arr;
        }
    }

    /**
     * 获取ajax过来的图片
     */
    public function getImg(Request $req){
        //print_r($req->goodpic);
        $name = 'a'.time().".jpg";
        $path = base_path("public/uploads/");
        $rs = $req->file('goodpic')->move($path,$name);
        if($rs){
            $pathname = $path.$name;
            //正则取后面地址
            $patt = "/(?=uploads).+$/";
            preg_match_all($patt,$pathname,$res);
            //print_r($res);
            echo "/".$res[0][0];
        }
    }

    //回收站展示:
    public function goodsdel($cat_id = 0){
        $goods = new Good();
        $cats = new Cat();
        $goodslist = [];
        $catlist = $this->getTree();
        //cat_id为0 则查出所有商品   不为0则查出相应栏目下商品
        if($cat_id != 0){
            $goodslist = $goods->join('cats','cats.cat_id','=','goods.cat_id')->where('goods.cat_id','=',$cat_id)->where('is_delete','=',1)->select(['goods.*','cats.cat_name'])->paginate(10);
        }else{
            $goodslist = $goods->join('cats','cats.cat_id','=','goods.cat_id')->where('is_delete','=',1)->select(['goods.*','cats.cat_name'])->paginate(10);
        }
        return view('admin/goodsdel',['goods'=>$goodslist,'catlist'=>$catlist]);
    }

    //从数据库中删除并且删除图片
    public function goodsdelpost($gid){
        $goods = Good::find($gid);
        //删除对应目录下的图片
        if($goods->delete()){
            return redirect('admins/grealdel');
        }
    }

    //还原商品
    public function hy($gid){
        $goods = Good::find($gid);
        $goods->is_delete = 0;
        $goods->save();
        return redirect('admins/grealdel');
    }
    //缩略图测试方法
    public function thumb(Request $req){

        $name = time().".jpg";
        $path = base_path("public/uploads/");
        $rs = $req->file('pic')->move($path,$name);
        if($rs) {
            $pathname = $path . $name;
            //正则取后面地址
            $patt = "/(?=uploads).+$/";
            preg_match_all($patt, $pathname, $res);
            //print_r($res);
            echo "/" . $res[0][0];
        }
        return "ok";
    }
    
    //ajax修改best字段
    public function best($id,$ok){
        $good = Good::find($id);
        if($ok == 1){
            //改为0
            $good->is_best = 0;
            $rs = $good->save();
            if($rs){echo 1;}else{echo 0;}
        }else{
            //改为1
            $good->is_best = 1;
            $rs = $good->save();
            if($rs){echo 1;}else{echo 0;}
        }
    }
    public function hot($id,$ok){
        $good = Good::find($id);
        if($ok == 1){
            //改为0
            $good->is_hot = 0;
            $rs = $good->save();
            if($rs){echo 1;}else{echo 0;}
        }else{
            //改为1
            $good->is_hot = 1;
            $rs = $good->save();
            if($rs){echo 1;}else{echo 0;}
        }
    }
    public function sale($id,$ok){
        $good = Good::find($id);
        if($ok == 1){
            //改为0
            $good->is_on_sale = 0;
            $rs = $good->save();
            if($rs){echo 1;}else{echo 0;}
        }else{
            //改为1
            $good->is_on_sale = 1;
            $rs = $good->save();
            if($rs){echo 1;}else{echo 0;}
        }
    }
    //后台首页
    public function index(){
        $orders = Order::join('Customers','customers.uid','=','orders.uid')->where('orders.status','<','3')->orderBy('orders.order_id','desc')->skip(0)->take(8)->select(['orders.*','customers.name'])->get();
        $weifu = Order::select(DB::raw('count(*) as count'))->where('status','=','0')->first();
        $yifa = Order::select(DB::raw('count(*) as count'))->where('status','=','2')->first();
        $yifu = Order::select(DB::raw('count(*) as count'))->where('status','=','1')->first();
        $end = Order::select(DB::raw('count(*) as count'))->first();
        //dd($weifu);
        return view('admin/index',['order'=>$orders,'weifu'=>$weifu,'yifu'=>$yifu,'yifa'=>$yifa,'end'=>$end]);
    }
}
